package com.sopt.friends.util;

import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.sopt.friends.db.FriendsDBHelper;
import com.sopt.friends.db.Constants.FriendsDB;
import com.sopt.friends.vo.FriendsBasicMember;
import com.sopt.friends.vo.FriendsCommonItem;
import com.sopt.friends.vo.FriendsPhone;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.format.DateUtils;

public class FriendsGroupManager {
	private Context c;
	private ContentResolver currentResolver;
	private SQLiteDatabase db;

	public  FriendsGroupManager(Context context,ContentResolver currentResolver) {
		this.c = context;
		this.currentResolver = currentResolver;
	}

	public SQLiteDatabase getDb() {
		return db;
	}

	public void setDb(SQLiteDatabase db) {
		this.db = db;
	}
	
	public ArrayList<FriendsCommonItem> getGroupList(){
		ArrayList<FriendsCommonItem> list = new ArrayList<FriendsCommonItem>();
		FriendsDBHelper helper = new FriendsDBHelper(c);
		db = helper.getReadableDatabase();
		
		Cursor cursor = db.query(FriendsDB.TABLE_GROUP, null, null, null, null, null, null);
		
		while(cursor.moveToNext()){
			FriendsCommonItem item = new FriendsCommonItem();
			String curGroupName = cursor.getString(cursor.getColumnIndex(FriendsDB.GROUP_NAME));
			String curGroupId = cursor.getString(cursor.getColumnIndex(FriendsDB.GROUP_SEQ));
			
			item.set_id(curGroupId);
			item.setTitle(curGroupName);
			
			list.add(item);
		}
		cursor.close();
		
		for(FriendsCommonItem curItem : list){
			String[]columns = {"count(*) as count"};
			String where = FriendsDB.GROUP_SEQ + " = " + curItem.get_id();
			
			if(Integer.parseInt(curItem.get_id())==1){
				cursor = db.query(FriendsDB.TABLE_MEMBER, columns, null, null, null, null, null);
			}else{
				cursor = db.query(FriendsDB.TABLE_MEMBER, columns, where, null, null, null, null);
			}
			
			if(cursor.moveToNext()){
				curItem.setNewcount(Integer.parseInt(cursor.getString(cursor.getColumnIndex("count"))));
			}
			cursor.close();
		}
		
		db.close();
		
		return list;
		
	}
	
	public ArrayList<FriendsBasicMember> getMemberList(String groupId){
		ArrayList<FriendsBasicMember> list = new ArrayList<FriendsBasicMember>();
		
		FriendsDBHelper helper = new FriendsDBHelper(c);
		
		db = helper.getReadableDatabase();
		
		String[]column = {FriendsDB.MEMBER_ID,FriendsDB.MEMBER_NAME,
				"date(strftime('%s'," + FriendsDB.MEMBER_REGDATE
				+ "),'unixepoch') as " + FriendsDB.MEMBER_REGDATE};
		String selection = "";
		if(Integer.parseInt(groupId)==1){
			selection = null;
		}else{
			selection = FriendsDB.GROUP_SEQ +" = " + groupId;
		}
		String orderby = FriendsDB.MEMBER_NAME;
		
		Cursor cursor = db.query(FriendsDB.TABLE_MEMBER, column, selection, null, null, null, orderby);
		
		while(cursor.moveToNext()){
			String contactId = cursor.getString(cursor.getColumnIndex(FriendsDB.MEMBER_ID));
			String displayName = cursor.getString(cursor.getColumnIndex(FriendsDB.MEMBER_NAME));
			String regdate = cursor.getString(cursor.getColumnIndex(FriendsDB.MEMBER_REGDATE));
			
			FriendsBasicMember item = new FriendsBasicMember();
			item.set_id(contactId);
			item.setDisplayname(displayName);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date regDate;
			try {
				regDate = format.parse(regdate);
			} catch (ParseException e) {
				regDate = new Date();
			}

			regdate = (String) DateUtils.getRelativeTimeSpanString(regDate
					.getTime());
			
			item.setRegdate(regdate);
			
			
			Uri uri = ContentUris.withAppendedId(
					ContactsContract.Contacts.CONTENT_URI, Long
							.parseLong(contactId));
			InputStream is = ContactsContract.Contacts
					.openContactPhotoInputStream(currentResolver, uri);
			Bitmap bitmap = BitmapFactory.decodeStream(is);

			item.setPhoto(bitmap);
			
			list.add(item);
		}
		cursor.close();
		
		for(FriendsBasicMember curMem:list){
			String[]columns = {FriendsDB.PHONE_NUMBER,FriendsDB.PHONE_TYPE};
			selection = FriendsDB.MEMBER_ID +" = " + curMem.get_id();
			
			cursor = db.query(FriendsDB.TABLE_PHONE, columns, selection, null, null, null, null);
			
			ArrayList<FriendsPhone> phoneList = new ArrayList<FriendsPhone>();
			while(cursor.moveToNext()){
				String curNumber = cursor.getString(cursor.getColumnIndex(FriendsDB.PHONE_NUMBER));
				String curType = cursor.getString(cursor.getColumnIndex(FriendsDB.PHONE_TYPE));
				phoneList.add(new FriendsPhone(curNumber,curType));
			}
			cursor.close();
			curMem.setPhoneList(phoneList);
		}
		
		
		db.close();
		
		return list;
		
	}
	
	
}
